﻿using System.Collections.ObjectModel;
using System.Windows;
using CarAppDto.CarType.CarDtos;
using CarAppClient.ViewModels;
using CarAppClient.Consts;
using CarAppClient.Helpers;
using D.UtilCore.Dtos;
using CarAppClient.Views.CarType.Views;
using CarAppClient.Controls.MessageUsercontrols;
using CarAppDto.Sys.AdminDtos;
using CarAppDto.CarType.BrandDtos;
using CarAppClient.Views.CarType.Views.Brand;
using CarAppDto.Common;
using D.UtilSqlsugarOrm;

namespace CarAppClient.Views.CarType.ViewModels.Brand
{
    public class BrandViewModel : BaseCrudViewModel<RepBrand>
    {
        #region 初始化页面

        public BrandViewModel()
        {
            LoadData();
        }

        public void LoadData()
        {
            Data = new ObservableCollection<RepBrand>();
            Query();
        }
        #endregion

        #region 查询条件

        private string? name;
        /// <summary>
        /// 名称
        /// </summary>
        public string? Name
        {
            get { return name; }
            set { name = value; RaisePropertyChanged(); }
        }

        #endregion

        #region 新增

        /// <summary>
        /// 新增
        /// </summary>
        protected override void Add(object? obj)
        {
            Page_Brand_Modify view = new Page_Brand_Modify(viewStatus:ViewStatus.Add);
            view.ShowDialog();

            // 刷新数据
            LoadData();
        }
        #endregion

        #region 单个删除

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="repCarType"></param>
        protected override async void Delete(RepBrand repCarType)
        {
            // 基础数据判断
            if (Data == null)
            {
                MsgUtil.Warning("暂无数据可删除");
                // 刷新数据
                LoadData();
                return;
            }

            if (repCarType == null)
            {
                MsgUtil.Warning("请选择要删除的数据"); return;
            }

            // 二次询问提示
            var messageBoxResult = MsgUtil.Question("确定删除吗？");
            if (messageBoxResult == MessageBoxResult.OK)
            {
                // 请求后端接口 
                var res = await HttpHelper<int>.DeleteAsync($"{ApiUrl.DeleteCarType}/{repCarType.Id}");
                if (res.IsSuccess)
                {
                    //  判断结果
                    MsgUtil.ShowMsg(res.Code, res.ErrorMsg, "删除成功");

                    // 刷新数据
                    LoadData();
                }
                else
                {
                    MsgUtil.ShowMsg(res.Code, res.ErrorMsg, "删除失败");
                }
            }
        }
        #endregion

        #region 批量删除

        protected override async void BatchDelete(object? obj)
        {
            // 基础数据判断
            if (Data == null)
            {
                MsgUtil.Warning("暂无数据可删除");
                // 刷新数据
                LoadData();
                return;
            }

            if (!Data.Any(m => m.IsSelected))
            {
                MsgUtil.Warning("请选择要删除的数据"); return;
            }

            // 二次询问提示
            var messageBoxResult = MsgUtil.Question("确定删除吗？");
            if (messageBoxResult == MessageBoxResult.OK)
            {
                // 获取要删除的数据 
                var ids = Data.Where(m => m.IsSelected).Select(m => m.Id).ToList();

                // 请求后端接口 
                var res = await HttpHelper<int>.DeleteAsync($"{ApiUrl.DeleteBrand}/{string.Join(",", ids)}");
                if (res.IsSuccess)
                {
                    //  判断结果
                    MsgUtil.ShowMsg(res.Code, res.ErrorMsg, "删除成功");

                    // 刷新数据
                    LoadData();
                }
                else
                {
                    MsgUtil.ShowMsg(res.Code, res.ErrorMsg, "删除失败");
                }
            }
        }
        #endregion

        #region 修改

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="vm"></param>
        protected override void Update(RepBrand? vm)
        {
            if (vm != null)
            {
                Page_Brand_Modify view = new Page_Brand_Modify(vm.Id,viewStatus:ViewStatus.Update);
                view.ShowDialog();

                // 刷新数据
                LoadData();
            }
        }
        #endregion

        #region 查询

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="repCarType"></param>
        protected override async void Query(object? obj = null)
        {
            if (Data != null)
            {
                Data.Clear();
            }
            else
            {
                Data = new ObservableCollection<RepBrand>();
            }

            // 请求后端接口 
            var res = await HttpHelper<PageModel<RepBrand>>.PostAsync(ApiUrl.QueryBrandPages, new ReqQueryBrandPage()
            {
                Name = Name,
                PageSize = PageSize,
                PageNo = PageNo
            });

            //  判断结果
            if (res.Code == StatusCodeEnum.Waring)
            {
                MsgUtil.Warning(res.ErrorMsg);
            }
            else if (res.Code == StatusCodeEnum.Error)
            {
                MsgUtil.Error(res.ErrorMsg);
            }
            else if (res.Code == StatusCodeEnum.ServerError)
            {
                MsgUtil.Error("服务器异常");
            }
            else if (res.Code == StatusCodeEnum.OK)
            {
                // 绑定数据源到UI 
                if (res.Data != null)
                {
                    foreach (var item in res.Data.Data)
                    {
                        item.IsSelected = false;
                        Data.Add(item);
                    }

                    TotalCount = res.Data.TotalCount;
                    TotalPage = res.Data.TotalPage;

                    base.RefPage();
                }
            }
            else
            {
                MsgUtil.Error(res.Code.ToString());
            }
        }
        #endregion

        #region 重置查询条件

        /// <summary>
        /// 重置
        /// </summary>
        /// <param name="obj"></param>
        protected override void Reset(object? obj)
        {
            Name = string.Empty;

            // 刷新数据
            LoadData();
        }
        #endregion

        #region 导出

        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="obj"></param>
        protected override void Export(object? obj)
        {

        }
        #endregion
    }
}
